#include <QListWidget>
#include <QCheckBox>
#include <QList>
#include <QLineEdit>
#include <QDebug>

#include "llcombobox.h"

class LLComboBoxPrivate
{
public:

    QStringList selectedDataList;
    QListWidget *pListWidget;
};

LLComboBox::LLComboBox(QWidget *parent)
    : QComboBox(parent)
    , m_pd(new LLComboBoxPrivate)
{
    m_pd->pListWidget = new QListWidget;
    m_pd->pListWidget->setFixedHeight(300);
    QPalette palette = m_pd->pListWidget->palette();
    palette.setColor(QPalette::Base, Qt::white);
    m_pd->pListWidget->setStyleSheet("background-color:#FFFFFF;color:#000000");

    connect(m_pd->pListWidget, SIGNAL(itemChanged(QListWidgetItem *)), this, SLOT(slot_itemChanged(QListWidgetItem *)));
    setView(m_pd->pListWidget);
    //设置Editable为true，lineEdit() 函数才不返回空
    setEditable(true);
}

void LLComboBox::addDataItem(const QString &text, const QVariant &userData)
{
    m_pd->pListWidget->blockSignals(true);
    QListWidgetItem *pItem = new QListWidgetItem(text, m_pd->pListWidget);
    pItem->setData(Qt::UserRole, userData);
    pItem->setCheckState(Qt::Unchecked);
    //设置QListWidgetItem 可交互且可以选中和取消选中
    pItem->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
    QComboBox::addItem(text);
    QComboBox::setCurrentText("");
    m_pd->pListWidget->blockSignals(false);

}

void LLComboBox::setSelectedData(const QStringList &selectedData)
{
    m_pd->selectedDataList.clear();
    m_pd->selectedDataList += selectedData;
    allItem.clear();
    allItem+=selectedData;
    m_pd->pListWidget->blockSignals(true);

    for (int var = 0; var < allItem.size(); ++var)
    {
        QListWidgetItem *pItem = m_pd->pListWidget->item(var);
        pItem->setCheckState(Qt::Checked);
    }
    m_pd->pListWidget->blockSignals(false);

    lineEdit()->setText("全部");
}

QStringList LLComboBox::selectedDataText()
{
    return m_pd->selectedDataList;
}

QList<QVariant> LLComboBox::selectedUserData()
{
    QList<QVariant> dataList;
    for (int var = 0; var < m_pd->selectedDataList.size(); ++var)
    {
        QListWidgetItem *pItem = m_pd->pListWidget->item(var);
        if(pItem->checkState() == Qt::Checked)
        {
            dataList.append(pItem->data(Qt::UserRole));
        }
    }
    return dataList;
}

void LLComboBox::slot_itemChanged(QListWidgetItem *item)
{
    if(m_pd->selectedDataList.contains(item->text()))//删除
    {
        if(item->text()=="全部"){
            m_pd->pListWidget->blockSignals(true);
            m_pd->selectedDataList.clear();

            for (int i=0;i<allItem.length();i++) {
                QListWidgetItem *pItem = m_pd->pListWidget->item(i);
                pItem->setCheckState(Qt::CheckState::Unchecked);
            }

            m_pd->pListWidget->blockSignals(false);
        }
        else if(m_pd->selectedDataList.contains("全部")){
            m_pd->selectedDataList.removeOne(item->text());

            m_pd->pListWidget->blockSignals(true);
            QListWidgetItem *pItem = m_pd->pListWidget->item(0);
            pItem->setCheckState(Qt::CheckState::Unchecked);

            m_pd->pListWidget->blockSignals(false);
            m_pd->selectedDataList.removeOne("全部");

        }
        else{
            m_pd->selectedDataList.removeOne(item->text());
        }
    }
    else//新增
    {
        m_pd->selectedDataList.append(item->text());

        if(item->text()=="全部")
        {
            m_pd->selectedDataList.clear();
            m_pd->selectedDataList += allItem;

            m_pd->pListWidget->blockSignals(true);

            for (int i=0;i<allItem.length();i++) {
                QListWidgetItem *pItem = m_pd->pListWidget->item(i);
                pItem->setCheckState(Qt::CheckState::Checked);
            }
            m_pd->pListWidget->blockSignals(false);

        }
        if(!m_pd->selectedDataList.contains("全部")&& m_pd->selectedDataList.length()==allItem.length()-1)
        {
            m_pd->selectedDataList.clear();
            m_pd->selectedDataList += allItem;

            m_pd->pListWidget->blockSignals(true);

            for (int i=0;i<allItem.length();i++) {
                QListWidgetItem *pItem = m_pd->pListWidget->item(i);
                pItem->setCheckState(Qt::CheckState::Checked);
            }
            m_pd->pListWidget->blockSignals(false);

        }
    }

    QString text = "";
    if(m_pd->selectedDataList.contains("全部"))
    {
        text = "全部";
    }
    else
    {
        for (int var = 0; var < m_pd->selectedDataList.size(); ++var)
        {
            text.append(m_pd->selectedDataList[var]);
            if(var < m_pd->selectedDataList.size() - 1)
            {
                text.append(",");
            }
        }
    }
    lineEdit()->setText(text);
}

void LLComboBox::DataClear(){
    m_pd->pListWidget->blockSignals(true);
    m_pd->pListWidget->clear();
    m_pd->pListWidget->blockSignals(false);
}

void LLComboBox::SetCurrentData(QString data){
    QList<QString> dataList=data.split(',');
    if(!dataList.contains("全部"))
    {
        for (int m=0;m<allItem.length();m++) {
            QListWidgetItem *pItem = m_pd->pListWidget->item(m);
            if(!dataList.contains(pItem->text())){
                pItem->setCheckState(Qt::CheckState::Unchecked);
            }
        }
    }

    QString text = "";
    if(m_pd->selectedDataList.contains("全部"))
    {
        text = "全部";
    }
    else
    {
        for (int var = 0; var < m_pd->selectedDataList.size(); ++var)
        {
            text.append(m_pd->selectedDataList[var]);
            if(var < m_pd->selectedDataList.size() - 1)
            {
                text.append(",");
            }
        }
    }

    lineEdit()->setText(text);
}
